﻿using Enums;
using SqlSugar;
using Untils;

namespace CommonBaseEntityDb.Db
{
    public class DbContext
    {
        /// <summary>
        /// 测试库
        /// </summary>
        public static SqlSugarScope TestSugarDb => new SqlSugarScope(new ConnectionConfig() { ConfigId = SqlConfigIdEnum.Test, DbType = DbType.SqlServer, ConnectionString = TestSugarConfig.ConnectionString, IsAutoCloseConnection = true }, db =>
        {
            if (TestSugarConfig.SqlTrace)
            {
                //里面可以循环
                db.GetConnection(SqlConfigIdEnum.Test).Aop.OnLogExecuting = (sql, p) =>
                {
                    //输出SQL
                    LoggerHelper.Info(string.Format("执行SQL:{0}", sql));
                    //输出参数
                    LoggerHelper.Info(string.Format("执行参数:{0}", string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))));
                };
            }
        });

        /// <summary>
        /// 模板库
        /// </summary>
        public static SqlSugarScope TemplateSugarDb => new SqlSugarScope(new ConnectionConfig() { ConfigId = SqlConfigIdEnum.Template, DbType = DbType.SqlServer, ConnectionString = TemplateSugarConfig.ConnectionString, IsAutoCloseConnection = true }, db =>
        {
            if (TemplateSugarConfig.SqlTrace)
            {
                //里面可以循环
                db.GetConnection(SqlConfigIdEnum.Template).Aop.OnLogExecuting = (sql, p) =>
                {
                    //输出SQL
                    LoggerHelper.Info(string.Format("执行SQL:{0}", sql));
                    //输出参数
                    LoggerHelper.Info(string.Format("执行参数:{0}", string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))));
                };
            }
        });
    }
}
